package com.tuojiahr.service.impl;

import java.util.Date;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tuojiahr.common.HibernatePageCommon;
import com.tuojiahr.domain.TuoJiaHrSpecialColumn;
import com.tuojiahr.domain.TuoJiaHrSpecialColumnEntry;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.SpecialEntryService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-10 下午10:54:27
 */

@Transactional
public class SpecialEntryServiceImpl extends HibernatePageCommon implements
		SpecialEntryService {

	//添加
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void addEntry(TuoJiaHrSpecialColumnEntry entry, long specialId) {
		TuoJiaHrSpecialColumn specialColumn = getHibernateTemplate().load(TuoJiaHrSpecialColumn.class, specialId);
		Date now = new Date();
		entry.setCreateTime(now);
		entry.setUpdateTime(now);
		entry.setParentColumn(specialColumn);
		getHibernateTemplate().persist(entry);
	}

	//更新
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateEntry(TuoJiaHrSpecialColumnEntry entry, long specialId) {
		entry.setUpdateTime(new Date());
		TuoJiaHrSpecialColumn specialColumn = getHibernateTemplate().load(TuoJiaHrSpecialColumn.class, specialId);
		entry.setParentColumn(specialColumn);
		getHibernateTemplate().update(entry);
	}

	//删除
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteEntryById(String ids) {
		String[] idArr = ids.split("\\|");
		for (String id : idArr) {
			getHibernateTemplate().delete(getEntryById(Long.valueOf(id)));
		}
	}

	//根据id查询
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public TuoJiaHrSpecialColumnEntry getEntryById(long entryId) {
		return getHibernateTemplate().load(TuoJiaHrSpecialColumnEntry.class, entryId);
	}

	//查询
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PageModel getEntryList(String keyWord, long specialId) {
		if(StringUtil.isNotBlank(keyWord)) {
			Object[] params = new Object[]{keyWord, specialId};
			return this.searchPaginated("from TuoJiaHrSpecialColumnEntry se where se.title like ? and se.parentColumn.id = ? order by se.orderNum", params);
		}
		return this.searchPaginated("from TuoJiaHrSpecialColumnEntry se where se.parentColumn.id = ? order by se.orderNum", specialId);
	}

}
